import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:inmotion_pro/config/r.dart';
import 'package:inmotion_pro/l10n/local.dart';
import 'package:inmotion_pro/pages/models/user_model.dart';
import 'package:inmotion_pro/pages/others/common_enum/vehicle_owner_type.dart';
import 'package:inmotion_pro/service/global_service.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';

/// 车辆授权用户列表
class AuthUserItemView extends StatelessWidget {
  // 用户模型
  final UserModel model;
  // 是否显示删除按钮
  final bool showDelete;
  // 删除事件
  final Function(UserModel)? deleteOnTap;

  const AuthUserItemView({
    super.key,
    required this.model,
    this.deleteOnTap,
    this.showDelete = true,
  });

  @override
  Widget build(BuildContext context) {
    double imageSize = 60.w;
    double radius = 12.w;
    bool isMe = model.uid == GlobalService.to.cacheUserInfo.value.uid;
    return ClipRRect(
      borderRadius: BorderRadius.circular(radius),
      child: Container(
        width: double.infinity,
        height: double.infinity,
        decoration: BoxDecoration(
          color: GrayColor.gray2,
          borderRadius: BorderRadius.circular(radius),
        ),
        child: Stack(
          children: [
            Align(
              alignment: Alignment.centerLeft,
              child: Padding(
                padding: EdgeInsets.only(
                  left: CONTENT_MARGIN,
                ),
                child: Container(
                  color: Colors.transparent,
                  alignment: Alignment.centerLeft,
                  child: ClipRRect(
                    borderRadius: BorderRadius.circular(radius),
                    child: model.avatar.isEmpty
                        ? Container(
                            width: imageSize,
                            height: imageSize,
                            color: GrayColor.gray3,
                            child: _defaultImage(
                              imageSize - 16.w,
                            ),
                          )
                        : AdvancedNetworkImage(
                            imageUrl: model.avatar,
                            width: imageSize,
                            height: imageSize,
                            placeholder: _defaultImage(imageSize),
                            fit: BoxFit.cover,
                          ),
                  ),
                ),
              ),
            ),
            Container(
              padding: EdgeInsets.only(
                left: imageSize + CONTENT_MARGIN * 2,
                top: CONTENT_MARGIN,
                bottom: CONTENT_MARGIN,
              ),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Container(
                        width: 20.w,
                        height: 20.w,
                        decoration: BoxDecoration(
                          color: LxCol.theme,
                          borderRadius: BorderRadius.circular(12.w),
                          border: Border.all(
                            color: Colors.white, // 边框颜色
                            width: 1.0, // 边框宽度
                          ),
                        ),
                        child: Center(
                            child: LxText(
                          model.ownerType == VehicleOwnerType.owner
                              ? local.primary
                              : local.secondary,
                          fontWeightEnum: LxFontWeightEnum.markSmall,
                        )),
                      ),
                      SizedBox(width: 5.w),
                      Container(
                        constraints: BoxConstraints(
                          maxWidth:
                              screenWidth - imageSize * 2 - 6 * CONTENT_MARGIN,
                        ),
                        child: LxText(
                          "${model.nickname.isEmpty ? model.name : model.nickname}${isMe ? " (${local.me})" : ""}",
                          color: Colors.white,
                          fontWeightEnum: LxFontWeightEnum.bodyLarge,
                        ),
                      ),
                    ],
                  ),
                  Container(
                    constraints: BoxConstraints(
                      maxWidth:
                          screenWidth - imageSize - 6 * CONTENT_MARGIN - 20.w,
                    ),
                    child: LxText(
                      model.createdAt,
                      color: Colors.white.withOpacity(0.6),
                      fontWeightEnum: LxFontWeightEnum.bodyMedium,
                    ),
                  ),
                ],
              ),
            ),
            Visibility(
              visible: showDelete,
              child: Positioned(
                top: CONTENT_MARGIN / 2,
                right: CONTENT_MARGIN / 2,
                child: GestureDetector(
                  onTap: () {
                    if (deleteOnTap != null) {
                      deleteOnTap!(model);
                    }
                  },
                  child: SizedBox(
                    width: CONTENT_MARGIN + 24.w,
                    height: CONTENT_MARGIN + 24.w,
                    child: Center(
                      child: Icon(
                        Icons.cancel,
                        color: Colors.white.withOpacity(0.7),
                        size: 24.w,
                      ),
                    ),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  // 默认应用图标
  Widget _defaultImage(double size) {
    return Center(
      child: Transform.translate(
        offset: const Offset(0, 0), // 向下偏移0像素
        child: Image.asset(
          R.unloginUserIcon,
          width: size - 16.w,
          fit: BoxFit.contain,
        ), // 你的组件
      ),
    );
  }
}
